package com.dayu.share.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.dayu.common.utils.StringUtils;
import com.dayu.share.domain.ShareGoods;
import com.dayu.share.mapper.ShareGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.share.mapper.ShareGoodsPriceMapper;
import com.dayu.share.domain.ShareGoodsPrice;
import com.dayu.share.service.IShareGoodsPriceService;

/**
 * 商品报价Service业务层处理
 * 
 * @author HK
 * @date 2021-04-08
 */
@Service
public class ShareGoodsPriceServiceImpl implements IShareGoodsPriceService 
{
    @Autowired
    private ShareGoodsPriceMapper shareGoodsPriceMapper;

    @Autowired
    private ShareGoodsMapper shareGoodsMapper;

    /**
     * 查询商品报价
     * 
     * @param priceId 商品报价ID
     * @return 商品报价
     */
    @Override
    public ShareGoodsPrice selectShareGoodsPriceById(Long priceId)
    {
        return shareGoodsPriceMapper.selectShareGoodsPriceById(priceId);
    }

    /**
     * 查询商品报价列表
     * 
     * @param shareGoodsPrice 商品报价
     * @return 商品报价
     */
    @Override
    public List<ShareGoodsPrice> selectShareGoodsPriceList(ShareGoodsPrice shareGoodsPrice)
    {
        return shareGoodsPriceMapper.selectShareGoodsPriceList(shareGoodsPrice);
    }

    /**
     * 新增商品报价
     * 
     * @param shareGoodsPrice 商品报价
     * @return 结果
     */
    @Override
    public int insertShareGoodsPrice(ShareGoodsPrice shareGoodsPrice)
    {
        return shareGoodsPriceMapper.insertShareGoodsPrice(shareGoodsPrice);
    }

    /**
     * 修改商品报价
     * 
     * @param shareGoodsPrice 商品报价
     * @return 结果
     */
    @Override
    public int updateShareGoodsPrice(ShareGoodsPrice shareGoodsPrice)
    {
        ShareGoodsPrice oldPrice = shareGoodsPriceMapper.selectShareGoodsPriceById(shareGoodsPrice.getPriceId());
        //如果价格有改动，记录入到旧价格列表
        if(shareGoodsPrice.getNewPrice() != null) {
            if(!shareGoodsPrice.getNewPrice().equals(oldPrice.getNewPrice())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dateStr = sdf.format(new Date());
                List<String> priceList = null;
                if(StringUtils.isEmpty(oldPrice.getOldPrice())) {
                    priceList = new ArrayList<>();
                }else {
                    priceList = (List<String>) JSONObject.parse(oldPrice.getOldPrice());
                }
                priceList.add(dateStr + ":" + shareGoodsPrice.getNewPrice());
                if(priceList.size() > 6) {
                    priceList.remove(0);
                }
                shareGoodsPrice.setOldPrice(JSONObject.toJSONString(priceList));
            }
        }
        return shareGoodsPriceMapper.updateShareGoodsPrice(shareGoodsPrice);
    }

    /**
     * 批量删除商品报价
     * 
     * @param priceIds 需要删除的商品报价ID
     * @return 结果
     */
    @Override
    public int deleteShareGoodsPriceByIds(Long[] priceIds)
    {
        return shareGoodsPriceMapper.deleteShareGoodsPriceByIds(priceIds);
    }

    /**
     * 删除商品报价信息
     * 
     * @param priceId 商品报价ID
     * @return 结果
     */
    @Override
    public int deleteShareGoodsPriceById(Long priceId)
    {
        return shareGoodsPriceMapper.deleteShareGoodsPriceById(priceId);
    }

    /**
     * 更新商品报价列表， 根据商品规格列表和成色生成
     * @param goodsId
     * @return
     */
    @Override
    public int updateGoodsPriceListByGoodsId(Long goodsId) {
        ShareGoods shareGoods = shareGoodsMapper.selectShareGoodsById(goodsId);
        List<List<Integer>> arr = (List<List<Integer>>) JSONObject.parse(shareGoods.getSpecsMap());
        Map<Integer, List<Integer>> specsMap = new HashMap<>();
        for (List<Integer> specsValue : arr) {
            //安卓机，颜色不进入报价
            if(!(shareGoods.getBrandId() == 1 || shareGoods.getBrandId() == 2)) {
                if(specsValue.get(0) == 2) {
                    continue;
                }
            }
            specsMap.computeIfAbsent(specsValue.get(0), k -> new ArrayList<>());
            specsMap.get(specsValue.get(0)).add(specsValue.get(1));
        }
        List<String> allList = specsMapCompose(specsMap);
        List<ShareGoodsPrice> newList = qualityCompose(allList, shareGoods.getQualityIds(), shareGoods);
        ShareGoodsPrice selectPrice = new ShareGoodsPrice();
        selectPrice.setGoodsId(shareGoods.getGoodsId());
        List<ShareGoodsPrice> oldList = shareGoodsPriceMapper.selectShareGoodsPriceList(selectPrice);
        if(oldList != null && oldList.size() > 0) {
            newList = oldContrastNew(oldList, newList);
        }
        if(newList.size() > 0) {
            shareGoodsPriceMapper.insertGoodsPriceList(newList);
        }
        return 1;
    }

    /**
     * 根据规格组，进行规格组合，组合出所有的规格
     * @param listMap
     * @return
     */
    private List<String> specsMapCompose(Map<Integer, List<Integer>> listMap) {
        List<String> oldSpecs = new ArrayList<>();

        for(Integer key : listMap.keySet()) {
            List<String> list = new ArrayList<>();
            List<Integer> valueIds = listMap.get(key);
            if(oldSpecs.size() == 0) {
                for(Integer values : valueIds) {
                    oldSpecs.add(values.toString());
                }
                continue;
            }

            for(Integer ids : valueIds) {
                for(String str : oldSpecs) {
                    String specs = str + "," + ids;
                    list.add(specs);
                }
            }
            oldSpecs = list;
        }
        System.out.println(oldSpecs);
        return oldSpecs;
    }

    /**
     * 获取所有规格成色报价列表
     * @param specsArr
     * @param quality
     * @param shareGoods
     * @return
     */
    private List<ShareGoodsPrice> qualityCompose(List<String> specsArr, String quality, ShareGoods shareGoods) {
        String[] qualityArr = quality.split(",");
        List<ShareGoodsPrice> list = new ArrayList<>();
        int sort = 1;
        for(int i = 0; i < specsArr.size(); i++) {
            for(int j = 0; j < qualityArr.length; j++) {
                ShareGoodsPrice shareGoodsPrice = new ShareGoodsPrice();
                shareGoodsPrice.setGoodsId(shareGoods.getGoodsId());
                shareGoodsPrice.setSpecsIds(specsArr.get(i));
                shareGoodsPrice.setQualityId(Long.parseLong(qualityArr[j]));
                shareGoodsPrice.setPriceSort(sort++);
                list.add(shareGoodsPrice);
            }
        }
        return list;
    }

    /**
     * 对比数据是否存在
     * @param oldList
     * @param newList
     * @return
     */
    private List<ShareGoodsPrice> oldContrastNew(List<ShareGoodsPrice> oldList, List<ShareGoodsPrice> newList) {
        for(int i = 0, len = newList.size(); i < len; i++) {
            ShareGoodsPrice newPrice = newList.get(i);
            for(ShareGoodsPrice oldPrice : oldList) {
                boolean bool = specsContrast(oldPrice.getSpecsIds(), newPrice.getSpecsIds());
                if(bool) {
                    if(oldPrice.getQualityId().equals(newPrice.getQualityId())) {
                        newList.remove(i);
                        len--;
                        i--;
                    }
                }
            }
        }
        return newList;
    }

    /**
     * 两个规格是否一样
     * @param specs1
     * @param specs2
     * @return
     */
    private boolean specsContrast(String specs1, String specs2) {
        specs1 = specs1 == null ? "" : specs1;
        specs2 = specs2 == null ? "" : specs2;
        if (specs1.equals(specs2)) {
            return true;
        }
        boolean bool = true;
        String[] specs1Arr = specs1.split(",");
        String[] specs2Arr = specs2.split(",");
        for(int i = 0; i < specs1Arr.length; i++) {
            for (int j = 0; j < specs2Arr.length; j++) {
                if(specs1Arr[i].equals(specs2Arr[j])) {
                    specs2Arr[j] = "";
                }
            }
        }

        for (String str : specs2Arr) {
            if (!"".equals(str)) {
                bool = false;
                break;
            }
        }
        return bool;
    }
}
